package fun.wlfj.servlet;

import java.io.IOException;
import java.util.LinkedList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import fun.wlfj.vo.VoteTask;

/**
 * Servlet implementation class newvoteservlet
 */
@WebServlet("/newvoteservlet")
public class newvoteservlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public newvoteservlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		String name = request.getParameter("votename");
		String message = null;
		if(name.equals("")) {
			message = "发布失败.请输入姓名!";
		}else {
			@SuppressWarnings("unchecked")
			LinkedList<VoteTask> taskList = (LinkedList<VoteTask>)this.getServletContext().getAttribute("TaskList");
			if(taskList == null){
				taskList = new LinkedList<VoteTask>();
				this.getServletContext().setAttribute("TaskList", taskList);
			}
			synchronized (taskList) {
				taskList.add(new VoteTask(name, taskList.size(), false));
			}
		}
		if(message == null) {
			message = "发布成功!";
		}
		request.setAttribute("message", message);
		request.getRequestDispatcher("manage").forward(request, response);
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
